package br.com.skolar.server;

import br.com.skolar.client.Service;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.persist.PersistFilter;
import com.google.inject.persist.jpa.JpaPersistModule;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;

public class GuiceConfig extends GuiceServletContextListener {
	private static Injector injector;
	
	@Override
	protected synchronized final Injector getInjector() {
		if (injector!=null)
			throw new Error("Injector already instantiated");
		injector = Guice.createInjector(new ServletModule() {
			@Override
			protected void configureServlets() {
				install(new SkolarModule());
				filter("*" + Service.SUFFIX).through(PersistFilter.class);
				serve("*" + Service.SUFFIX).with(GuiceRemoteServiceServlet.class);
				install(new JpaPersistModule("default")
					.addFinder(QuestionRepositoryImpl.QuestionFinder.class));
			}
		});
		return injector;
	}
}